package com.cincc.cinccimdemo.viewmodel;

import android.text.TextUtils;

import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.cincc.cinccimdemo.common.Constants;
import com.cincc.cinccimdemo.common.MyUtils;
import com.cincc.cinccimdemo.common.audio.MediaPlayerUtils;
import com.cincc.cinccimdemo.common.callback.OnGetMoreDataListener;
import com.cincc.cinccimdemo.model.ConversationInfo;
import com.cincc.cinccimdemo.model.MessageInfo;
import com.cincc.cinccimdemo.model.MessageInfoUI;
import com.cincc.cinccimdemo.model.OperationEntity;
import com.cincc.im.core.IMSipPhoneCtrl;
import com.cincc.im.model.dbmodel.GroupMessageInfo;
import com.cincc.im.model.dbmodel.P2PMessageInfo;
import com.cincc.im.model.dbmodel.SequenceInfo;
import com.cincc.im.model.enums.ImSendType;
import com.cincc.im.util.ImManager;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ConversationViewModel extends ViewModel {

    String messageTime = "";

    private MutableLiveData<List<MessageInfoUI>> conversationListLiveData;

    public MutableLiveData<List<MessageInfoUI>> conversationListLiveData(ConversationInfo conversationInfo) {
        if (conversationListLiveData == null) {
            conversationListLiveData = new MutableLiveData<>();
        }
        ImManager.getInstance().getWorkHandler().post(() -> {
            if (ImSendType.isP2P(conversationInfo.imSendType)) {
                List<P2PMessageInfo> p2PHistory = IMSipPhoneCtrl.getInstance().getP2PHistory(conversationInfo.targetId, Constants.DEFAULT_LOAD_CONVERSATION_SIZE, messageTime);
                if (!p2PHistory.isEmpty()) {
                    messageTime = p2PHistory.get(0).messageTime;
                }
                List<MessageInfoUI> list1 = new ArrayList<>();
                for (P2PMessageInfo item : p2PHistory) {
                    MessageInfo messageInfo = MyUtils.getMessageInfo(item, conversationInfo.imSendType);
                    MessageInfoUI ui = new MessageInfoUI(messageInfo);
                    list1.add(ui);
                }
                conversationListLiveData.postValue(list1);
            } else {
                List<GroupMessageInfo> groupHistory = IMSipPhoneCtrl.getInstance().getGroupHistory(conversationInfo.targetId, Constants.DEFAULT_LOAD_CONVERSATION_SIZE, messageTime);
                if (!groupHistory.isEmpty()) {
                    messageTime = groupHistory.get(0).messageTime;
                }
                List<MessageInfoUI> list1 = new ArrayList<>();
                for (GroupMessageInfo item : groupHistory) {
                    MessageInfo messageInfo = MyUtils.getMessageInfo(item, conversationInfo.imSendType);
                    MessageInfoUI ui = new MessageInfoUI(messageInfo);
                    list1.add(ui);
                }
                conversationListLiveData.postValue(list1);
            }
        });

        return conversationListLiveData;
    }

    /**
     * 下拉刷新调用
     *
     * @param conversationInfo 会话对象
     * @return 消息列表
     */
    private MutableLiveData<List<MessageInfoUI>> moreConversationListLiveData;

    public MutableLiveData<List<MessageInfoUI>> loadMoreLiveData(ConversationInfo conversationInfo) {
        if (moreConversationListLiveData == null) {
            moreConversationListLiveData = new MutableLiveData<>();
        }
        if (!TextUtils.isEmpty(messageTime)) {
            if (ImSendType.isP2P(conversationInfo.imSendType)) {
                List<P2PMessageInfo> p2PHistory = IMSipPhoneCtrl.getInstance().getP2PHistory(conversationInfo.targetId, Constants.DEFAULT_LOAD_CONVERSATION_SIZE, messageTime);
                if (!p2PHistory.isEmpty()) {
                    messageTime = p2PHistory.get(0).messageTime;
                }
                List<MessageInfoUI> list1 = new ArrayList<>();
                for (P2PMessageInfo item : p2PHistory) {
                    MessageInfo messageInfo = MyUtils.getMessageInfo(item, conversationInfo.imSendType);
                    MessageInfoUI ui = new MessageInfoUI(messageInfo);
                    list1.add(ui);
                }
                moreConversationListLiveData.setValue(list1);
            } else {
                List<GroupMessageInfo> groupHistory = IMSipPhoneCtrl.getInstance().getGroupHistory(conversationInfo.targetId, Constants.DEFAULT_LOAD_CONVERSATION_SIZE, messageTime);
                if (!groupHistory.isEmpty()) {
                    messageTime = groupHistory.get(0).messageTime;
                }
                List<MessageInfoUI> list1 = new ArrayList<>();
                for (GroupMessageInfo item : groupHistory) {
                    MessageInfo messageInfo = MyUtils.getMessageInfo(item, conversationInfo.imSendType);
                    MessageInfoUI ui = new MessageInfoUI(messageInfo);
                    list1.add(ui);
                }
                moreConversationListLiveData.postValue(list1);
            }
        }
        return moreConversationListLiveData;
    }

    public void getMoreData(ConversationInfo conversationInfo, OnGetMoreDataListener onGetMoreDataListener) {
        if (!TextUtils.isEmpty(messageTime)) {
            ImManager.getInstance().getWorkHandler().post(() -> {
                if (ImSendType.isP2P(conversationInfo.imSendType)) {
                    List<P2PMessageInfo> p2PHistory = IMSipPhoneCtrl.getInstance().getP2PHistory(conversationInfo.targetId, Constants.DEFAULT_LOAD_CONVERSATION_SIZE, messageTime);
                    if (!p2PHistory.isEmpty()) {
                        messageTime = p2PHistory.get(0).messageTime;
                    }
                    List<MessageInfoUI> list1 = new ArrayList<>();
                    for (P2PMessageInfo item : p2PHistory) {
                        MessageInfo messageInfo = MyUtils.getMessageInfo(item, conversationInfo.imSendType);
                        MessageInfoUI ui = new MessageInfoUI(messageInfo);
                        list1.add(ui);
                    }
                    onGetMoreDataListener.onCallback(list1);
                } else {
                    List<GroupMessageInfo> groupHistory = IMSipPhoneCtrl.getInstance().getGroupHistory(conversationInfo.targetId, Constants.DEFAULT_LOAD_CONVERSATION_SIZE, messageTime);
                    if (!groupHistory.isEmpty()) {
                        messageTime = groupHistory.get(0).messageTime;
                    }
                    List<MessageInfoUI> list1 = new ArrayList<>();
                    for (GroupMessageInfo item : groupHistory) {
                        MessageInfo messageInfo = MyUtils.getMessageInfo(item, conversationInfo.imSendType);
                        MessageInfoUI ui = new MessageInfoUI(messageInfo);
                        list1.add(ui);
                    }
                    onGetMoreDataListener.onCallback(list1);
                }
            });
        }

    }


    private MutableLiveData<MessageInfoUI> conversationLiveData;

    public MutableLiveData<MessageInfoUI> conversationLiveData() {
        if (conversationLiveData == null) {
            conversationLiveData = new MutableLiveData<>();
        }
        return conversationLiveData;
    }

    public void clearHistory() {
        List<MessageInfoUI> list1 = new ArrayList<>();
        conversationListLiveData.postValue(list1);
    }

    private MutableLiveData<MessageInfoUI> updateConversationLiveData;

    public MutableLiveData<MessageInfoUI> updateConversationLiveData() {
        if (updateConversationLiveData == null) {
            updateConversationLiveData = new MutableLiveData<>();
        }
        return updateConversationLiveData;
    }

    public void setConversationData(List<MessageInfoUI> list) {
        conversationListLiveData.postValue(list);
    }

    private MutableLiveData<Boolean> keyboardShowingLiveData;

    public MutableLiveData<Boolean> keyboardShowingLiveData() {
        if (keyboardShowingLiveData == null) {
            keyboardShowingLiveData = new MutableLiveData<>();
        }
        return keyboardShowingLiveData;
    }

    private MutableLiveData<OperationEntity> operationLiveData;

    public MutableLiveData<OperationEntity> operationLiveData() {
        if (operationLiveData == null) {
            operationLiveData = new MutableLiveData<>();
        }
        return operationLiveData;
    }

    private MutableLiveData<String> recallLiveData;

    public MutableLiveData<String> recallLiveData() {
        if (recallLiveData == null) {
            recallLiveData = new MutableLiveData<>();
        }
        return recallLiveData;
    }

    private MutableLiveData<String> deleteLiveData;

    public MutableLiveData<String> deleteLiveData() {
        if (deleteLiveData == null) {
            deleteLiveData = new MutableLiveData<>();
        }
        return deleteLiveData;
    }

    MessageInfoUI uiMessage;

    /**
     * 播放离线语音
     *
     * @param message 消息对象
     */
    public void playAudioMessage(MessageInfoUI message) {
        if (message == null) {
            return;
        }
        if (uiMessage != null && uiMessage.equals(message)) {
            message.setPlaying(false);
            updateConversationLiveData().setValue(message);
            MediaPlayerUtils.getInstance().stop();
            uiMessage = null;
            return;
        }
        if (uiMessage != null && !Objects.equals(uiMessage, message) && uiMessage.isPlaying()) {
            uiMessage.setPlaying(false);
            updateConversationLiveData().setValue(uiMessage);
        }
        uiMessage = message;
        String path = uiMessage.getMessageInfo().localPath;
        playAudio(path);
        message.setPlaying(true);
        updateConversationLiveData().setValue(message);
    }

    private void playAudio(String path) {
        MediaPlayerUtils.getInstance().setMediaPlayInfoListener(mediaPlayer -> {
            if (uiMessage != null) {
                uiMessage.setPlaying(false);
                updateConversationLiveData().postValue(uiMessage);
            }
        });
        MediaPlayerUtils.getInstance().startPlay(path);
    }
}
